<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of shareModel
 * 分享跟踪模块
 * @author xlp
 */
class shareModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'user_share';
    }

    /*
     * 获取分享数据
     * @param $uid int UID
     * @param $type string 类型标记，请参照config/share_inc.php下的配置信息
     * @param $fid int 分享源ID
     * @return array
     */

    public function getDetailInfo($uid, $type, $fid) {
        return $this->where(array('uid' => $uid, 'type' => $type, 'fid' => $fid))->find();
    }

    /*
     * 添加分享数据
     * @param $uid int UID
     * @param $type string 类型标记，请参照config/share_inc.php下的配置信息
     * @param $fid int 分享源ID
     * @return int 添加到分享表内的主键值
     */

    public function addShare($uid, $type, $fid) {
        $parentId = $this->_getShareSource($fid, $type);
        $init = array(
            'uid' => $uid,
            'fid' => $fid,
            'parent_id' => $parentId,
            'type' => $type,
            'share_count' => 1,
            'hits_count' => 0,
            'reply_count' => 0,
            'infotime' => TIME
        );
        if ($parentId) {//存在引用源UID，则将源UID转发数+1
            $parentArr = $this->_getParent($parentId);
            //将全体的转发源转发数+1
            if ($parentArr) {
                $this->where("id IN (" . implode(',', $parentArr) . ")")->setInc('reply_count', 1);
            }
        }
        return $this->insert($init);
    }

    /*
     * 更新分享数据
     * @param $uid int UID
     * @param $type string 类型标记，请参照config/share_inc.php下的配置信息
     * @param $fid int 分享源ID
     * @param $field string 需要更新的字段：share_count(分享统计)、hits_count(点击统计)、reply_count(转发统计)
     * @return int 操作影响的记录数
     */

    public function updateShareData($uid, $type, $fid, $field) {
        if (!in_array($field, array('share_count', 'hits_count', 'reply_count'))) {
            return false;
        }
        return $this->where(array('uid' => $uid, 'type' => $type, 'fid' => $fid))->setInc($field, 1);
    }

    /*
     * 更新分享数据被消耗次数
     * @param $shareid int shareid
     * @param $count int 消耗次数
     * @return int 操作影响的记录数
     */

    public function updateUseCount($shareid, $count = 1) {
        return $this->where(array('id' => $shareid))->setInc('is_used', $count);
    }

    /*
     * 根据fid取出所有的转发父级，可以一直追溯到最原始的转发源
     * @param $parentId int 当前parent_id
     * @return array 父级转发源id集合
     */

    private function _getParent($parentId) {
        static $arr = array();
        $rs = $this->field('id,parent_id')->where(array('id' => $parentId))->findAll();
        $arr[] = $parentId;
        if ($rs) {
            foreach ($rs as $v) {
                if ($v['parent_id']) {
                    $this->_getParent($v['parent_id']);
                }
            }
        }
        return $arr;
    }

    /*
     * 根据资料从cookie中获取转发源ID
     * @param $fid int FID
     * @param $type string 当前fid
     * @return int 如果数据校验正确会返回shareid，否则返回0
     */

    private function _getShareSource($fid, $type) {
        $share = myCookie('share_' . $type . '_' . $fid);
        $shareId = 0;
        if ($share) {
            $share = explode("\t", getDecode($share));
            //确认转发源
            //fid,type,shareid
            if (isset($share[0]) && isset($share[1]) && isHave($share[2]) && $share[0] == $fid && $share[1] == $type) {
                $shareId = intval($share[2]);
            }
        }
        return $shareId ? $shareId : 0;
    }

}
